package com.bmob.lover.ui.fragment.lover

import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.lifecycle.OnLifecycleEvent
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.loadmore.BaseLoadMoreView
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.theone.common.constant.BundleConstant
import com.theone.common.ext.*
import com.bmob.lover.R
import com.bmob.lover.app.ext.showDarkEmpty
import com.bmob.lover.app.ext.showDarkError
import com.bmob.lover.app.widget.LoadMoreView
import com.bmob.lover.app.widget.OnViewPagerListener
import com.bmob.lover.app.widget.PagerLayoutManager
import com.bmob.lover.app.widget.LoverVideoPlayer
import com.bmob.lover.data.model.bean.Lover
import com.bmob.lover.ui.adapter.Lover2Adapter
import com.bmob.lover.ui.fragment.base.BaseSwipePagerFragment
import com.bmob.lover.viewmodel.LoverViewModel
import com.chad.library.adapter.base.module.LoadMoreModule
import com.qmuiteam.qmui.widget.QMUITopBarLayout
import com.theone.mvvm.ext.qmui.addLeftCloseImageBtn
import kotlin.math.abs

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-04-22 14:00
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class Lover2Fragment :
    BaseSwipePagerFragment<Lover, LoverViewModel>() {

    companion object {
        fun newInstance(data: ArrayList<Lover>, position: Int, page: Int): Lover2Fragment {
            return Lover2Fragment().apply {
                arguments = Bundle().apply {
                    putParcelableArrayList(BundleConstant.DATA, data)
                    putInt(BundleConstant.POSITION, position)
                    putInt(BundleConstant.PAGE, page)
                }
            }
        }
    }

    private val mLoverList: ArrayList<Lover>? by getValue(BundleConstant.DATA)
    private val mPosition: Int? by getValue(BundleConstant.POSITION)
    private val mPage: Int? by getValue(BundleConstant.PAGE)

    private var mPlayPosition = 0

    private var mIsVisible = false

    override fun showTopBar(): Boolean = arguments != null

    override fun isDarkPage(): Boolean = true

    override fun translucentFull(): Boolean = true

    override fun isNeedChangeStatusBarMode(): Boolean = false

    override fun isStatusBarLightMode(): Boolean = false

    override fun createAdapter(): BaseQuickAdapter<Lover, *> = Lover2Adapter(showTopBar())

    override fun isLazyLoadData(): Boolean = false

    override fun isAutoRefresh(): Boolean = !showTopBar()

    override fun QMUITopBarLayout.initTopBar() {
       setBackgroundAlpha(0)
        addLeftCloseImageBtn(R.drawable.mz_comment_titlebar_ic_close_light)
    }

    override fun initView(root: View) {
        super.initView(root)
        GSYVideoManager.instance().isNeedMute = false
    }

    override fun initAdapter() {
        getAdapter().run {
            if (this is LoadMoreModule) {
                loadMoreModule.loadMoreView = getAdapterLoadMoreView()
                loadMoreModule.setOnLoadMoreListener(this@Lover2Fragment)
            }
        }
    }

    override fun requestServer() {
        mLoverList.notNull({
            onFirstLoadSuccess(it)
            mPosition?.let { position ->
                mPlayPosition = position
                getRecyclerView().scrollToPosition(position)
            }
            getViewModel().run {
                isVideo = true
                mPage?.let {
                    page = it
                }
                isFresh = false
                isFirst = false
            }
        }, {
            super.requestServer()
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

    }

    override fun createObserver() {
        super.createObserver()
        if (isAutoRefresh())
            mEventVm.isToOpenOrCloseDrawer().observe(this, Observer {
                if (mPlayPosition >= 0 && mIsVisible) {
                    if (it) {
                        GSYVideoManager.onPause()
                    } else {
                        GSYVideoManager.onResume()
                    }
                }
            })
    }

    override fun getLayoutManager(): RecyclerView.LayoutManager =
        PagerLayoutManager(context, LinearLayoutManager.VERTICAL).apply {
            setOnViewPagerListener(object : OnViewPagerListener {

                override fun onInitComplete(view: View?) {
                    startPlayVideo(mPlayPosition, view)
                }

                override fun onPageRelease(isNext: Boolean, position: Int, view: View?) {
                    view?.findViewById<LoverVideoPlayer>(R.id.video_player)?.release()
                }

                override fun onPageSelected(position: Int, isBottom: Boolean, view: View?) {
                    if (mPlayPosition != position
                    ) {
                        startPlayVideo(position, view)
                    }
                }
            })
        }

    private fun startPlayVideo(position: Int, view: View?) {
        mPlayPosition = position
        view?.findViewById<LoverVideoPlayer>(R.id.video_player)?.let {
            if (it.isVisible()) {
                it.startPlayLogic()
            }
        }
    }

    override fun onLazyResume() {
        super.onLazyResume()
        mIsVisible = true
        if ((!showTopBar() && mEventVm.isToOpenOrCloseDrawer().value != true) || showTopBar()) {
            (getAdapter() as Lover2Adapter).onResume()
        }
    }

    override fun onPause() {
        mIsVisible = false
        (getAdapter() as Lover2Adapter).onPause()
        super.onPause()
    }

    @Override
    override fun onDestroy() {
        Log.e(com.theone.common.ext.TAG, "onDestroy: " + showTopBar())
//        (mAdapter as Lover2Adapter).onDestroy()
        super.onDestroy()
    }

    override fun onFetchTransitionConfig(): TransitionConfig = SCALE_TRANSITION_CONFIG

}